//JavaScript Framework 2.0 Code
try {
	Type.registerNamespace('com.yonyou.ncmm_pac_pda.LoginPageController');
	com.yonyou.ncmm_pac_pda.LoginPageController = function() {
		com.yonyou.ncmm_pac_pda.LoginPageController.initializeBase(this);
		this.initialize();
	}

	function com$yonyou$ncmm_pac_pda$LoginPageController$initialize() {

	}

	function com$yonyou$ncmm_pac_pda$LoginPageController$evaljs(js) {
		eval(js)
	}

	function btnlogin_onclick2(sender, args) {
		var username = $id('txtusername').get('value');
		var userpwd = $id('txtpassword').get('value');
		if(!username) {
			$toast('用户名不能为空');
			return;
		}
		if(!userpwd) {
			$toast('密码不能为空');
			return;
		}
		var v = $id("switch0").get("value");
		//离线登录
		if(v == "off" || v == "OFF") {
			$js.showLoadingBar({
				msg: "正在登录，请稍后..."
			});
			var whereSql = "where user='" + username + "' and password='" + userpwd + "'";
			NCMMPubUtil.queryVO(USERVO, whereSql, function(data) {
				if(NCMMPubUtil.isEmpty(data)) {
					var whereSql1 = "where user='" + username + "'";
					NCMMPubUtil.queryVO(USERVO, whereSql1, function(data1) {
						if(NCMMPubUtil.isEmpty(data1)) {
							$js.hideLoadingBar();
							$alert("首次登陆请选择在线登录！");
						} else {
							$js.hideLoadingBar();
							$js.toast("离线用户名或密码错误！");
						}
					});
				} else {
					$cache.write("username", username);
					$cache.write("password", userpwd);
					$js.hideLoadingBar();
					$js.toast('登录成功！');
					$view.open({
						"viewid": "com.yonyou.ncmm_pac_pda.HomePage", //目标页面（首字母大写）全名，
						"isKeep": "false"
					});
				}
			});

		} else {
			$ctx.dataCollect();
			var host = $cache.read('host');
			var port = $cache.read('port');
			if(!host || !port) {
				$toast('请先设置NC服务器信息');
				return;
			}
			$cache.write("username", username);
			$cache.write("password", userpwd);

			var state = $app.getNetworkState() || {};
			if(state.available != 'true') {
				$js.toast("网络连接错误！");
				return;
			}
			$app.checkNetWorkReachable(host, port, function(param, args) {
				$js.hideLoadingBar();
				if(args.is_reachable != 'true') {
					$js.toast("服务器地址无法连通！");
					return;
				}
			});

			$js.showLoadingBar({
				msg: "正在登录，请稍后..."
			});
			/**
			 * @{link AppService.js}
			 */
			$app.login(username, userpwd, function(caller, args) {
				//			$js.hideLoadingBar();
				args = args || {};
				var login_result = args.login_result || {};
				if(!!login_result && typeof(login_result) == 'string') {
					login_result = $stringToJSON(login_result);
				}
				if(parseInt(login_result.code) != 1000) {
					$js.toast(login_result.msg);
					return;
				}
				var response = login_result.response || '登录失败';
				if(typeof(response) == 'string' && JSON.isJSONString(response)) {
					response = $stringToJSON(response);
				}
				if(typeof(response) == 'object') {
					var retString = response['return'] || {};
					var returnCode = retString.returnCode || -1;

					if(parseInt(returnCode) == 1111) {
						loginCallBack(username, userpwd);
						//					$js.toast('登录成功！');
					} else {
						$js.hideLoadingBar();
						var desc = '登录' + (retString.returnDesc || "失败");
						$js.toast(desc);
					}
				} else {
					$js.hideLoadingBar();
					$js.toast(response);
				}

			}, function(caller, args) {
				$js.hideLoadingBar();
				args = args || {};
				var login_result = args.login_result || {};
				if(!!login_result && typeof(login_result) == 'string') {
					login_result = $stringToJSON(login_result);
				}
				if(parseInt(login_result.code) != 1000) {
					$js.toast(login_result.msg);
				} else {
					$js.toast('未知错误，登录失败！');
				}
			});
		}

	}

	function loginCallBack(username, userpwd) {
		//登录成功后，初始化对照表
		NCMMDBUtil.initMappingTable();
		window.uploadTotal = 0;
		window.uploadSum = 0;
		window.uploadSum = window.uploadSum + 1;
		var wheresql = " where location='SINGLE' and datatype='BD' and ncbillcode='PDASTOCKORG'";
		var mapTable = NCMMPubUtil.queryVONoCallback(AppmappingheadVO, wheresql);
		var condition = {};
		if(NCMMPubUtil.isNotEmpty(mapTable)) {
			if(NCMMPubUtil.isNotEmpty(mapTable[0]["downloadtime"])) {
				var downtime = mapTable[0]["downloadtime"];
				downtime = downtime.replace("年", "-");
				downtime = downtime.replace("月", "-");
				downtime = downtime.replace("日", "");
				condition["creationtime"] = ">='" + downtime + "'";
			}
		}
		NCMMDBUtil.processDocDataFromNC2017(StockorgVO, "库存组织", condition, [], 0, function() {

		});
		var userVO = {
			user: username,
			password: userpwd
		};
		NCMMPubUtil.queryVO(USERVO, "where user='" + username + "'", function(data) {
			if(NCMMPubUtil.isEmpty(data)) {
				NCMMPubUtil.insertVO(USERVO, userVO);
			} else {
				NCMMPubUtil.updateVO(USERVO, userVO);
			}
		});
		$js.toast('登录成功！');
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.HomePage", //目标页面（首字母大写）全名，
			"isKeep": "false"
		});

	}

	function lblregister_onclick(sender, args) {
		$ctx.dataCollect();
		var host = $cache.read('host');
		var port = $cache.read('port');
		if(!host || !port) {
			$toast('请先设置NC服务器信息');
			return;
		}
		var deviceInfo = $device.getDeviceInfo();
		var jsondevice = $stringToJSON(deviceInfo);
		//var cachedeviceid = $cache.read("deviceuniqueid");
		//if(NCMMPubUtil.isNotEmpty(cachedeviceid)) {
		//	$toast("该设备已注册，不需要重复注册！");
		//	return;
		//}
		var deviceid = jsondevice.deviceid;
		var jsonparam = {
			"user": NCMMHttpUtil.USER,
			"password": NCMMHttpUtil.PASSWORD,
			"exsystemcode": NCMMHttpUtil.EXSYSTEMCODE,
			"modulecode": NCMMHttpUtil.ICMODULECODE,
			"deviceuniqueid": deviceid,
			"devicecode": ""
		};
		//$alert(jsonparam);
		//var jsonparam = {
		//	"user": "fj",
		//	"password": "123456a",
		//	"exsystemcode": "MM",
		//	"deviceuniqueid": deviceid,
		//	"devicecode": ""
		//};
		//调用是否达到最大授权数接口服务
		var checkurl = NCMMHttpUtil.getQuestURL("checkpermission");
		//$alert(checkurl);
		//"http://172.16.4.242:3750/service/checkpermission"
		NCMMHttpUtil.sendHTTPRequest(checkurl, jsonparam, function(data, flag) {
			if(flag > 0) {
				if(flag == 1) { //成功		
					if($isJSONObject(data)) {
						var returnjson = $stringToJSON(data);
						var issuccess = returnjson.issuccess;
						if(issuccess == "0") {
							$toast(returnjson.err_msg);
							return;
						} else if(issuccess == "1") {
							//调用设备注册
							var regurl = NCMMHttpUtil.getQuestURL("deviceregister");
							//"http://172.16.4.242:3750/service/deviceregister"
							NCMMHttpUtil.sendHTTPRequest(regurl, jsonparam, function(data, flag) {
								var returnjson = $stringToJSON(data);
								var issuccess = returnjson.issuccess;
								if(issuccess == "1") {
									$cache.write("devicecode", returnjson.devicecode);
									$cache.write("deviceuniqueid", returnjson.deviceuniqueid);
								}
								$toast(returnjson.ret_msg);
							});
						}
						return;
					} else {
						$alert("服务器连接失败!"+$jsonToString(data));
						return;
					}
				} else {
					$alert("服务器连接失败!"+$jsonToString(data));
					return;
				}
			} else {
				$alert("服务器连接失败!"+$jsonToString(data));
				return;
			}
		});
	}

	function lblloginsetting_onclick2(sender, args) {
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.LoginSettingPage2", //目标页面（首字母大写）全名
			isKeep: "true" //打开新页面的同时是否保留当前页面，true为保留，false为不保留
		});
	}

	function onPageLoad(sender, args) {
		NCMMPubUtil.createTable(USERVO);
		var username = $cache.read("username") ? $cache.read("username") : "";
		var settinginfo = {
			"username": username,
		};
		$ctx.push(settinginfo);
		if(NCMMPubUtil.isEmpty(username)) {
			$id("txtusername").focus();
		} else {
			$id("txtpassword").focus();
		}
	}

	function loginTypeChanged(sender, args) {
		var v = $id("switch0").get("value");
		if(v == "on") {
			$id("label1").set("text", "在线登录");
		} else {
			$id("label1").set("text", "离线登录")
		}

	}
	com.yonyou.ncmm_pac_pda.LoginPageController.prototype = {
		initialize: com$yonyou$ncmm_pac_pda$LoginPageController$initialize,
		evaljs: com$yonyou$ncmm_pac_pda$LoginPageController$evaljs
	};
	com.yonyou.ncmm_pac_pda.LoginPageController.registerClass('com.yonyou.ncmm_pac_pda.LoginPageController', UMP.UI.Mvc.Controller);
} catch(e) {
	$e(e);
}